//
//  BrowseCountBtn.m
//  陶瓷
//
//  Created by 王戈 on 15/8/26.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "BrowseCountBtn.h"

@interface BrowseCountBtn ()

@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UIImageView *heartView;

@end

@implementation BrowseCountBtn

- (UILabel *)countLabel {
    if (_countLabel == nil) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.font = [UIFont systemFontOfSize:14];
    }
    return _countLabel;
}

- (UIImageView *)heartView {
    if (_heartView == nil) {
        _heartView = [[UIImageView alloc] init];
        _heartView.image = [UIImage imageNamed:@"img_xin1"];
    }
    return _heartView;
}


- (void)setCount:(NSString *)count {
    if (self.countLabel) {
        NSString *countStr = [NSString stringWithFormat:@"%@",count];
        _countLabel.text = countStr;
    }
    [self layoutIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.width && self.height) {
        if (![self.subviews containsObject:_countLabel]) {
            [self addSubview:_countLabel];
            [self addSubview:self.heartView];
        }
        
        CGFloat maxLabelWidth = self.width * 0.5;
        CGFloat maxLabelHeight = self.height;
        
        CGSize labelSize = [_countLabel.text sizeWithfont:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(maxLabelWidth , maxLabelHeight)];
        
        CGFloat labelX = self.width - labelSize.width - 15;
        _countLabel.frame = CGRectMake(labelX, 0, labelSize.width, self.height);
        
        CGFloat heartWidth = self.height * 0.8;
        CGFloat heartY = (self.height - heartWidth ) * 0.5;
        CGFloat heartX = labelX - heartWidth - 5;
        _heartView.frame = CGRectMake(heartX, heartY, heartWidth, heartWidth);
    }
}


@end
